package com.ctos.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctos.common.Result;
import com.ctos.entity.Category;
import com.ctos.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService service;

    /**
     * 分页查询显示
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<Page> pageQuery(Integer page, Integer pageSize){
        Page<Category> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        //排序 降序
        qw.orderByAsc(Category::getSort);
        service.page(pageInfo,qw);
        return Result.success(pageInfo);
    }

    /**
     * 添加菜品or添加套餐
     * @param category
     * @return
     */
    @PostMapping()
    public Result<String > save(@RequestBody Category category){
        service.save(category);
        return Result.success("success");
    }

    /**
     * 修改菜品信息
     * @param category
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody Category category){

        service.updateById(category);
        return Result.success("success");
    }

    /**
     * 删除菜品
     * @param id
     * @return
     */
    @DeleteMapping()
    public Result<String> deleteById(@RequestParam("ids") Long id){
        service.remove(id);
        return Result.success("success");
    }

    /**
     * 获取菜品列表
     * @param category
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> getList(Category category){

        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(category.getType()!=null,Category::getType, category.getType());
        //添加排序条件
        qw.orderByDesc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = service.list(qw);

        return Result.success(list);
    }

}
